<?php
namespace app\api\controller;

use app\api\logic\Weixin as loginWeixin;

/**
 * 登陆认证流程
 * 1.
 * 调用wx.login，注册用户账号
 * 2. 调用wx.getUserInfo，获取授权信息，
 *
 * @author Wuhui
 *        
 */
class Weixin extends Base {

    public function index () {
        return '';
    }
    
    // 微信小程序注册与登陆
    public function loginApplet () {
        $post = request()->only(['js_code', 'cid', 'encrypted_data', 'iv'], 'post');
        $weixin = new loginWeixin();
        $weixin->loginApplet($post);
    }

    /**
     * 更新昵称 、头像等信息
     *
     * @param array $userinfo            
     * @return boolean
     */
    public function updateUserinfo () {
        $post = request()->only(['nickname', 'gender', 'language', 'city', 'province', 'country', 'avatarurl'], 'post');
        
        $post['update_time'] = $this->timeStamp;
        $result = db('user')->where(['id' => $this->accessData['uid']])->update($post);
        
        if($result <= 0) {
            err('UPDATE ERROR');
        }
        
        suc();
    }
    
    // 微信小程序支付回调  file_get_contents('php://input');
    public function wxNotify () {

//         if(empty($xml = @$GLOBALS['HTTP_RAW_POST_DATA'])) {
//         //if(empty($xml = file_get_contents('php://input'))) {
//             err('HTTP_RAW_POST_DATA not data');
//         }
        
        // 调试
        //logWriteInFile('wxNotify', $xml);

        // 计算出admin_id用于 - 查询出支付参数(appid等)
        $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        
        $str = base64_decode($result['attach']);
        $data = parseKeyVal($str);
        
        // 引入配置参数
        $weixin = new loginWeixin();
        $weixin->wxPayConfig($data['id']);
                

        $notify = new WxNotify();
        $notify->Handle(false);
    }
    
    /**
     * 微信回调测试接口
     * 
     * @param json $_POST['data']   json格式的数据，请参考下面的测试数据
     * 
     * 测试数据
        {
            "debug": "1",
            "appid": "wxb5e8870519774974",
            "attach": "aWQ9MTI3Nw==",
            "bank_type": "CFT",
            "cash_fee": "1",
            "fee_type": "CNY",
            "is_subscribe": "N",
            "mch_id": "1481015162",
            "nonce_str": "w6cdtgjprjuos14nv90duqh9q5fh9r7b",
            "openid": "o0sMa0WCLxXlqESs5-FIeZf5DrWw",
            "out_trade_no": "2017110219302710430386587",
            "result_code": "SUCCESS",
            "return_code": "SUCCESS",
            "sign": "E4D1FCA9D227E5068433EF578F65C4E7",
            "time_end": "20171102193033",
            "total_fee": "1",
            "trade_type": "JSAPI",
            "transaction_id": "4200000009201711022002310555"
        }
     */
    public function wxNotifyTest() {
        $post = '        {
            "debug": "1",
            "appid": "wxb5e8870519774974",
            "attach": "aWQ9MTI3Nw==",
            "bank_type": "CFT",
            "cash_fee": "1",
            "fee_type": "CNY",
            "is_subscribe": "N",
            "mch_id": "1481015162",
            "nonce_str": "w6cdtgjprjuos14nv90duqh9q5fh9r7b",
            "openid": "o0sMa0WCLxXlqESs5-FIeZf5DrWw",
            "out_trade_no": "2017110219302710430386587",
            "result_code": "SUCCESS",
            "return_code": "SUCCESS",
            "sign": "E4D1FCA9D227E5068433EF578F65C4E7",
            "time_end": "20171102193033",
            "total_fee": "1",
            "trade_type": "JSAPI",
            "transaction_id": "4200000009201711022002310555"
        }';

        //$post = request()->only(['data'], 'post');
        $notify = new WxNotify();
        $msg = '测试';
        $notify->NotifyProcess(json_decode($post, true), $msg);
        echo $msg;
    }
    
    // 获取access token
    public function getAccessToken () {
        $weixin = new Weixin();
        $res = $weixin->getAccessToken($this->accessData['aid']);
        suc('success', $res);
    }
}